﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TAMEZL
    {
        public TAMEZL()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMEINST();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        { 
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            base.NewCommand(sender, e);
        }
       
        public void CopyMDCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return; 
            dwData = TMV.VAMD.TBMain.NewRow();
            dwData["AMMMANDANT"] = dwMaster["AMEMANDANT"];
            dwData["AMMNRKRS"] = dwMaster["AMENRKRS"];
            dwData["AMMLFDNR"] = dwMaster["AMELFDNR"];
            dwData["AMMPOS"] = dwMaster["AMEPOS"];
            dwData["AMMMODNR"] = dwMaster["AMEMODNR"];
            SResult = TMV.VAMD.QAMD04(dwData);
            foreach (DataRow dwAM in SResult.Tables[0].Rows)
            {
                dwData = TBMain.NewRow();
                dwData["AMEMANDANT"] = dwAM["AMMMANDANT"];
                dwData["AMENRKRS"] = dwAM["AMMNRKRS"];
                dwData["AMELFDNR"] = dwAM["AMMLFDNR"];
                dwData["AMEPOS"] = dwAM["AMMPOS"];
                dwData["AMEMODNR"] = dwAM["AMMMODNR"];
                TMV.DAME03(dwData);
            }
            filter = "AMEMODNR='" + MODNR + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows.CopyToDataTable().Rows)
            {
                foreach (DataRow dwAM in SResult.Tables[0].Rows)
                {
                    row["AMENRKRS"] = dwAM["AMMNRKRS"];
                    row["AMELFDNR"] = dwAM["AMMLFDNR"];
                    row["AMEPOS"] = dwAM["AMMPOS"];
                    row["AMEMODNR"] = dwAM["AMMMODNR"];
                    string BDEKEN = dwAM["AMMNRKRS"].ToString() + dwAM["AMMLFDNR"].ToString().Substring(2) + dwAM["AMMMODNR"].ToString().Substring(1) + row["AMEBDEKEN"].ToString().Substring(6);
                    row["AMEBDEKEN"] = BDEKEN;
                    TMV.InsertAMEINST(row);
                }
            } 
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
             
            base.SaveCommand(sender, e);
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }
        public override void OnBDEKENChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.AMEBDEKEN = Helpers.Helper.Left(BDEKEN.Trim().PadRight(8), 8);
            base.OnBDEKENChg();
        }
        public override void OnMESCHLChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.AMEMESCHL = Helpers.Helper.Left(MESCHL.Trim().PadRight(2), 2);
            base.OnMESCHLChg();
        }

  

    }
}
